function checknull(str) {
	if (str == "") {
		return true;
	} else {
		return false;
	}
}
function checkmin(str, min) {
	if (str.getBytes() < min) {
		return true;
	} else {
		return false;
	}
}
function checkmax(str, max) {
	if (str.getBytes() > max) {
		return true;
	} else {
		return false;
	}
}
String.prototype.getBytes = function() {
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);
};
function checkln(str) {
	var valid = /^[a-zA-Z][0-9a-zA-Z]*$/;
	return (valid.test(str));
}
function checklandn(str) {
	var valid = /^[0-9a-zA-Z]*$/;
	return (valid.test(str));
}
function checkl(str) {
	var valid = /^[a-zA-Z]*$/;
	return (valid.test(str));
}
function checknumber(str) {
	var valid = /[0-9]$/;
	return (valid.test(str));
}
function checkmail(str) {
	var valid = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	return (valid.test(str));
}
function checkblankspace(str) {
	if (str.indexOf(" ") >= 0) {
		return true;
	}
	return false;
}
function mustNum(Str) {
	return str.replace(/\D/g,"");
}
function checkdate(str) {
	var valid = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$/;
	return (valid.test(str));
}
//验证昵称
function checknickname() {
	var str = $("#nickname").val();
	if(checknull(str)){
		showErrMsg("nickname", "昵称不能为空");
		return false;
	}
	if(checkmin(str, 1)){
		showErrMsg("nickname", "昵称不能小于4个字符");
		return false;
	}
	if(checkmax(str, 10)){
		showErrMsg("nickname", "昵称长度不能超过10个字符");
		return false;
	}
	if (checkblankspace(str)) {
		showErrMsg("nickname", "昵称不能含有空格");
		return false;
	}
	hiddMsg("nickname");
	return true;
}
//验证密码
function checkpwd() {
	var str = $("#password").val();
	if(checknull(str)) {
		showErrMsg("password", "密码不能为空");
		return false;
	}
	if(checkmin(str, 6)) {
		showErrMsg("password", "密码长度不能小于6个字符");
		return false;
	}
	if(checkmax(str, 32)) {
		showErrMsg("password", "密码长度不能超过32个字符");
		return false;
	}
	if(!checklandn(str)) {
		showErrMsg("password", "密码只能包含字母和数字");
		return false;
	}
	hiddMsg("password");
	return true;
}
//重复密码
function checkrepwd() {
	var str = $("#repwd").val();
	var password = $("#password").val();
	if(!checkpwd(password)) {
		showErrMsg("repwd", "密码还不正确");
		return false;
	}
	if(password != str) {
		showErrMsg("repwd", "两次密码不一致");
		return false;
	}
	hiddMsg("repwd");
	return true;
}
//
//验证年龄
function checkage() {
	var str = $("#age").val();
	if(checknull(str)) {
		showErrMsg("age", "请填写您的年龄");
		return false;
	}
	if(Number(str) < 1 || Number(str) > 130) {
		showErrMsg("age", "请正确填写您的年龄");
		return false;
	}
	hiddMsg("age");
	return true;
}
//验证省
function checkprovince() {
	var str = $("#s1").val();
	if(str == "请选择") {
		showErrMsg("s1", "请选择所在省");
		return false;
	}
	hiddMsg("s1");
	return true;
}
//验证市
function checkcity() {
	var str = $("#s2").val();
	if(str == "请选择") {
		showErrMsg("s2", "请选择所在市");
		return false;
	}
	hiddMsg("s2");
	return true;
}
//checkemail
function checkemail() {
	var str = $("#email").val();
	if(!checkmail(str)) {
		showErrMsg("email", "请输入正确的email");
		return false;
	}
	hiddMsg("email");
	return true;
}
//初始化性别select
function setupSelect() {
	var sexSelect = $("#sex");
	var sexArray = ["男", "女"];
	for ( var i = 0; i < 2; i++) {
		sexSelect.append("<option value='"+sexArray[i]+"'>"+sexArray[i]+"</option>");
	}
}
function checkaccount1() {
	var str = $("#account").val();
	if(checknull(str)){
		showErrMsg("account", "账号不能为空");
		return false;
	}
	if(checkmin(str, 4)){
		showErrMsg("account", "账号不能小于4个字符");
		return false;
	}
	if(checkmax(str, 20)){
		showErrMsg("account", "账号不能超过20个字符");
		return false;
	}
	if(!checkln(str)){
		showErrMsg("account", "账号必须以字母开头只能使用字母和数字");
		return false;
	}
	return true;
}
//验证账号
function checkaccount(){
	var flag = $("#accountflag");
	var str = $("#account").val();
	if (checkaccount1()) {
		$.ajax({
			type : "POST",
			url : "ChatUserPo",
			data : "m=checkaccount&account="+str,
			success : function (data) {
			if(data=="true"){
				hiddMsg("account");
				flag.val(true);
			}else{
				showErrMsg("account", "此账号以注册，请换名重试");
				flag.val(false);
			}
		},
		error : function () {
			flag.val(true);
		}
		});
	}
}
//验证性别
function checksex() {
	var str = $("#sex").val();
	if(str == "0") {
		showErrMsg("sex", "请选择您的性别");
		return false;
	}
	hiddMsg("sex");
	return true;
}
//接受协议
function checkaccept() {
	var str = $("#checkbox").attr("checked");
	if (str) {
		hiddMsg("ck");
		return true;
	} else {
		showErrMsg("ck", "您未同意用户协议");
		return false;
	}

}
//验证日期
function checkborn() {
	var str = $("#born").val();
	if (str==null || str=="" || checkdate(str)) {
		hiddMsg("born");
		return true;
	}
	showErrMsg("born", "日期格式不正确");
	return false;
}
//身高
function checkheight() {
	var str = $("#height").val();
	if(checkmax(str, 3)) {
		showErrMsg("height", "你也太高了吧");
		return false;
	}
	hiddMsg("height");
	return true;
}
//体重
function checkweight() {
	var str = $("#weight").val();
	if (checkmax(str, 3)) {
		showErrMsg("weight", "有点不正常吧");
		return false;
	}
	hiddMsg("weight");
	return true;
}
//毕业院校
function checkshcool() {
	var str = $("#school").val();
	if (checkmax(str, 50)) {
		showErrMsg("school", "长度不能大于50字符");
		return false;
	}
	hiddMsg("school");
	return true;
}
//职业
function checkoccupation() {
	var str = $("#occupation").val();
	if (checkmax(str, 50)) {
		showErrMsg("occupation", "长度不能大于50字符");
		return false;
	}
	hiddMsg("occupation");
	return true;
}
//电话
function checkphone() {
	var str = $("#phone").val();
	if (checkmax(str, 15)) {
		showErrMsg("phone", "长度不能大于15字符");
		return false;
	}
	hiddMsg("phone");
	return true;
}
//个人介绍
function checkdescription() {
	var str = $("#description").val();
	if(checkmax(str, 200)) {
		showErrMsg("description", "长度不能超过200个字符");
		return false;
	}
	hiddMsg("description");
	return true;
}
//换验证码
function changecaptcha() {
	var url = "Captcha?r="+Date.parse(new Date());
	$("#capcode").attr("src",url);
	return false;
}
//验证验证码
function checkcaptcha1() {
	var str = $("#captcha").val();
	if(!checkl(str) || checknull(str) || checkmin(str, 4)) {
		showErrMsg("captcha", "验证码错误");
		return false;
	}
	return true;
}
function checkcaptcha() {
	var str = document.getElementById("captcha").value;
	var tipDiv = document.getElementById("tips");
	var cpfIpt = document.getElementById("captchaflag");
	if(!checkl(str)) {
		showErrMsg("captcha", "验证码错误");
		changecaptcha();
		cpfIpt.value = false;
		if (tipDiv!=null) {
			tipDiv.innerHTML = "验证码错误";
		}
		return;
	}
	var xmlhttp;
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}else{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4&&xmlhttp.status==200){
			var str2=xmlhttp.responseText.replace(/\s+/g,"");
			if(str2=="true"){
				cpfIpt.value = true;
				if (tipDiv!=null) {
					tipHide();
				}
				hiddMsg("captcha");
			}else{
				changecaptcha();
				cpfIpt.value = false;
				if (tipDiv!=null) {
					tipShow("验证码错误");
				}
				showErrMsg("captcha", "验证码错误");
			}
		}
	};
	xmlhttp.open("POST","ChatUserPo",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("m=checkcaptcha&captcha="+str);
}

//根据id显示str信息
function showMsg(idStr, str) {
	var msgSpan = $("#"+idStr+"msg");
	if (msgSpan!=null) {
		msgSpan.attr("class", "tip");
		msgSpan.css("color", "#7f91b7");
		msgSpan.html(str);
	}
}
//根据id显示错误str信息
function showErrMsg(idStr, str) {
	var msgSpan = $("#"+idStr+"msg");
	if (msgSpan!=null) {
		msgSpan.attr("class", "tip");
		msgSpan.css("color", "red");
		msgSpan.html(str);
	}
}
function hiddMsg(idStr) {
	var msgSpan = $("#"+idStr+"msg");
	if (msgSpan!=null) {
		msgSpan.attr("class", "tipHide");
	}
}